'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 登陆服务器
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../consts/code');
var userModel = require('../../../models/userModel');
var roleModel = require('../../../models/roleModel');
var authService = require('../../../services/authService');
var configUtil = require('../../../util/configUtil.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
    this.globalChannelService = app.get('globalChannelService');
};

/**
 * 调试工具用户登陆
 */
Handler.prototype.entry = function (msg, session, next) {
    var signed_key = msg.signed_key;
    var uid = msg.uid;

    var debug_status = configUtil.load('auth').debug;
    if (!debug_status) {
        console.error('just can be run for debug');
        return next(null, {
            code: Code.ENTRY_CAN_NOT_PASS
        });
    }

    if (!signed_key) {
        return next(null, {
            code: Code.ENTRY_TOKEN_EXPIRE
        });
    }

    if (signed_key !== 'iosdofi23070') {
        return next(null, {
            code: Code.ENTRY_TOKEN_EXPIRE
        });
    }

    var auth_uid;
    var channel_id = 10001;
    async.waterfall(
        [
            function (cb) {
                session.set('auth_uid', uid);
                session.set('channel_id', channel_id);
                session.pushAll(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

Handler.prototype.hello = function (msg, session, next) {
    var name = msg.name;

    return next(null, {
        code: 200,
        info: 'hello ' + name
    });
};
